package com.itheima.web.controller.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.domain.front.ExamQuestion;
import com.itheima.domain.store.Question;
import com.itheima.web.controller.BaseServlet;
import com.itheima.web.controller.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @program: mm_exam
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-20 11:08
 **/
@WebServlet("/exam/*")
public class ExamServlet extends BaseServlet {
    public Result applyPaper(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String json = JSON.parseObject(req.getInputStream(), String.class);
        JSONObject jsonObject = JSON.parseObject(json);
        String memberId = jsonObject.getString("memberId");
        JSONArray jsonArray = jsonObject.getJSONArray("results");
        List<ExamQuestion> examQuestionList = jsonArray.toJavaList(ExamQuestion.class);
        boolean flag = examService.applyPaper(memberId, examQuestionList);
        return new Result("添加成功", flag);

    }

    public Result getPaper(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //调用逻辑层api
        List<Question> questionList = examService.getPaper();
        Result result;
        result = new Result("试卷生成成功", questionList);

        return result;

    }
}
